package classloader;

import java.io.IOException;
import java.io.InputStream;

import sun.misc.Launcher;

public class ClassLoaderTest {

	public static void main(String[] args) throws Exception{
		System.out.println(new Launcher().getClassLoader());
		System.out.println(Launcher.getLauncher().getClassLoader());
		System.out.println(Launcher.getBootstrapClassPath());
		test_parentLoader();
		test_sysProperty();
		test_myClassLoader();
	}
	
	public static void test_parentLoader() {
		VerifyError entity = new VerifyError();
		System.out.println(entity.getClass().getClassLoader());
		
		System.out.println(Number.class.getClassLoader());
		
		ClassLoader loaderSys = ClassLoaderTest.class.getClassLoader();
		System.out.println(loaderSys.toString());
		/*--sun.misc.Launcher$AppClassLoader@4e0e2f2a--*/
		
		
	}
	
	public static void test_sysProperty() {
		System.out.println(System.getProperty("java.ext.dirs"));
		System.out.println(System.getProperty("java.class.path"));
	}
	
	public static void test_myClassLoader() throws ClassNotFoundException {
		MyClassLoader myLoader = new MyClassLoader();
		System.out.println(myLoader.getParent());
		Object obj = myLoader.loadClass("founder.ChannelKeyUtil");
		System.out.println(((Class)obj).getClassLoader());
		System.out.println(obj);
//		System.out.println(((ChannelKeyUtil)obj).websiteName2Hash("adf"));
	}
	
	
}

class MyClassLoader extends ClassLoader{
	@Override
	public Class<?> loadClass(String name) throws ClassNotFoundException{
		try {
			String fileName = name.substring(name.lastIndexOf(".")+1)+".class";
			Class c = getClass();
			InputStream is = getClass().getResourceAsStream(fileName);
			if(is==null) {
				return super.loadClass(name);
			}
			byte[] b = new byte[is.available()];
			is.read(b);
			return defineClass(name, b, 0, b.length);
		}catch(IOException e) {
			throw new ClassNotFoundException(name);
		}
	}
}
